﻿using System.Linq;
using Myotragus.Data.RepositoryExtensions;
using Myotragus.Store.Model.Tests.Domain;
using NHibernate.Criterion;
using NUnit.Framework;

namespace Myotragus.Store.Model.Tests
{
  [TestFixture]
  public class LocalizableRepositoryFixtures
  {
    [SetUp]
    public void SetUp()
    {
      FixtureHelper.InitializeServices();
      FixtureHelper.InitializeEntities(new[] { typeof(ILocalizableObject) });
      FixtureHelper.InitializeSession(new[] { typeof(ILocalizableObject) });
    }

    [Test]
    public void LocalizableProvider()
    {
      var repo = FixtureHelper.Container.Resolve<ILocalizableObjectRepo>();
      var list = repo.FindAll(10, 0, -1, Order.Asc("Name"));

      Assert.IsTrue(list.Any());
      Assert.AreEqual(list.Count(), repo.CountAll());

      var matchPattern = new { Name = "Erich" };
      var matchList = repo.FindMatching(10, matchPattern, 0, -1, Order.Desc("Name"));

      Assert.AreEqual(matchList.Count(), repo.CountMatching(10, matchPattern));

      matchPattern = new { Name="Los la borrachos pisan nunca que vuelve de ilesa" };
      matchList = repo.FindMatching(10, matchPattern, 0, -1, Order.Asc("Name"));
      Assert.AreEqual(1, matchList.Count());
      Assert.AreEqual(1, repo.CountMatching(10, matchPattern));

      var likePattern = new { Name = "Erich" };
      var likeList = repo.FindLike(10, likePattern, 0, -1, Order.Asc("Name"));

      Assert.AreEqual(likeList.Count(), repo.CountLike(10, likePattern));

      likePattern = new { Name="borrachos" };
      likeList = repo.FindLike(10, likePattern, 0, -1, Order.Desc("Name"));
      Assert.Greater(likeList.Count(), 0);
      Assert.AreEqual(likeList.Count(), repo.CountLike(10, likePattern));
    }

    [Test]
    public void DefaultsLocalizableProvider()
    {
      var repo = FixtureHelper.Container.Resolve<IDefaultsLocalizableObjectRepo>();
      var list = repo.FindAll(10, 0, -1, Order.Asc("Name"));

      Assert.IsTrue(list.Any());
      Assert.AreEqual(list.Count(), repo.CountAll());

      var matchPattern = new { Name = "Erich" };
      var matchList = repo.FindMatching(10, matchPattern, 0, -1, Order.Desc("Name"));

      Assert.AreEqual(matchList.Count(), repo.CountMatching(10, matchPattern));

      matchPattern = new { Name = "Los la borrachos pisan nunca que vuelve de ilesa" };
      matchList = repo.FindMatching(10, matchPattern, 0, -1, Order.Asc("Name"));
      Assert.AreEqual(1, matchList.Count());
      Assert.AreEqual(1, repo.CountMatching(10, matchPattern));

      var likePattern = new { Name = "Erich" };
      var likeList = repo.FindLike(10, likePattern, 0, -1, Order.Asc("Name"));

      Assert.AreEqual(likeList.Count(), repo.CountLike(10, likePattern));

      likePattern = new { Name = "borrachos" };
      likeList = repo.FindLike(10, likePattern, 0, -1, Order.Desc("Name"));
      Assert.Greater(likeList.Count(), 0);
      Assert.AreEqual(likeList.Count(), repo.CountLike(10, likePattern));
    }

    [Test]
    public void PrintOne()
    {
      var repo = FixtureHelper.Container.Resolve<ILocalizableObjectRepo>();
      var list = repo.FindAll(10, 0, -1, Order.Asc("Name")).First();

      FixtureHelper.Print(list);

      var drepo = FixtureHelper.Container.Resolve<IDefaultsLocalizableObjectRepo>();
      var dlist = repo.FindAll(10, 0, -1, Order.Asc("Name")).First();

      FixtureHelper.Print(dlist);
    }
  }
}
